import { Injectable } from '@nestjs/common';
import { ResponseUtil } from '../common/utils/response.util';

@Injectable()
export class ServiceService {
  public serviceData = {
    list: [
      {
        name: 'etcd',
        type: 'ClusterIP',
        clusterIp: 'None',
        ports: [
          {
            port: 2379,
            targetPort: 2379,
            protocol: 'TCP',
          },
          {
            port: 2380,
            targetPort: 2380,
            protocol: 'TCP',
          },
        ],
        createTime: '2025-08-25T09:08:09Z',
      },
      {
        name: 'karmada-apiserver',
        type: 'NodePort',
        clusterIp: '10.90.196.124',
        ports: [
          {
            port: 5443,
            targetPort: 5443,
            protocol: 'TCP',
          },
        ],
        createTime: '2025-08-25T09:08:11Z',
      },
      {
        name: 'karmada-aggregated-apiserver',
        type: 'ClusterIP',
        clusterIp: '10.90.4.140',
        ports: [
          {
            port: 443,
            targetPort: 443,
            protocol: 'TCP',
          },
        ],
        createTime: '2025-08-25T09:08:42Z',
      },
      {
        name: 'kube-controller-manager',
        type: 'ClusterIP',
        clusterIp: '10.90.10.41',
        ports: [
          {
            port: 10257,
            targetPort: 10257,
            protocol: 'TCP',
          },
        ],
        createTime: '2025-08-25T09:08:47Z',
      },
      {
        name: 'karmada-webhook',
        type: 'ClusterIP',
        clusterIp: '10.90.238.102',
        ports: [
          {
            port: 443,
            targetPort: 8443,
            protocol: 'TCP',
          },
        ],
        createTime: '2025-08-25T09:08:54Z',
      },
      {
        name: 'karmada-dashboard-api',
        type: 'ClusterIP',
        clusterIp: '10.90.10.112',
        ports: [
          {
            port: 8000,
            targetPort: 8000,
            protocol: 'TCP',
          },
        ],
        createTime: '2025-08-26T01:41:01Z',
      },
      {
        name: 'karmada-dashboard-web',
        type: 'NodePort',
        clusterIp: '10.90.119.140',
        ports: [
          {
            port: 8000,
            targetPort: 8000,
            protocol: 'TCP',
          },
        ],
        createTime: '2025-08-26T01:41:01Z',
      },
    ],
    continueToken: null,
  };
  async postListServices() {
    return ResponseUtil.success(this.serviceData);
  }

  public serviceDetail = {
    serviceBasicInfo: {
      namespace: 'karmada-system',
      name: 'etcd',
      createTime: '2025-08-25T09:08:09Z',
      serviceType: 'ClusterIP',
      clusterIp: 'None',
      keepSession: 'None',
    },
    ports: [
      {
        name: 'client',
        protocol: 'TCP',
        servicePort: 2379,
        containerPort: 2379,
        access: 'ClusterIP',
      },
      {
        name: 'server',
        protocol: 'TCP',
        servicePort: 2380,
        containerPort: 2380,
        access: 'ClusterIP',
      },
    ],
    annotations: {
      'app.kubernetes.io/description':
        'MySQL database service for payment processing',
      'app.kubernetes.io/maintainer': 'dev-ops@example.com',
      'app.kubernetes.io/documentation':
        'https://docs.example.com/databases/mysql',
      'app.kubernetes.io/source': 'https://github.com/example/mysql-deployment',
      'database.admin.username': 'admin',
      'database.character-set': 'utf8mb4',
      'database.collation': 'utf8mb4_unicode_ci',
    },
    labels: {
      'app.kubernetes.io/name': 'mysql',
      'app.kubernetes.io/instance': 'mysql-prod',
      'app.kubernetes.io/version': '8.0',
      'app.kubernetes.io/component': 'database',
      'app.kubernetes.io/part-of': 'payment-system',
      db: 'postgresql',
      edition: 'enterprise',
      version: '14.5',
      replica: 'primary',
      storage: 'ssd',
    },
    labelSelector: {
      'app.kubernetes.io/name': 'mysql',
      'app.kubernetes.io/component': 'database',
      environment: 'production',
      tier: 'backend',
      replicas: '3',
      'karmada.io/bootstrapping': 'service-defaults',
      db: 'postgresql',
      edition: 'enterprise',
      backup: 'daily',
      encryption: 'enabled',
      'connection-limit': '1000',
    },
    servicePods: [
      {
        podName: 'etcd-0',
        podIp: '10.244.1.15',
        nodeName: 'federation02',
        status: 'Running',
      },
      {
        podName: 'etcd-1',
        podIp: '10.244.2.37',
        nodeName: 'federation03',
        status: 'Running',
      },
      {
        podName: 'etcd-2',
        podIp: null,
        nodeName: 'federation01',
        status: 'Pending',
      },
      {
        podName: 'etcd-3',
        podIp: '10.244.3.89',
        nodeName: 'federation04',
        status: 'Running',
      },
      {
        podName: 'etcd-4',
        podIp: '10.244.0.12',
        nodeName: 'federation05',
        status: 'Terminating',
      },
    ],
    events: [],
  };

  postDetailService() {
    return ResponseUtil.success(this.serviceDetail);
  }

  postDeleteService(body: any) {
    const currentIndex = this.serviceData.list.findIndex(
      (item) => item.name === body.name,
    );
    this.serviceData.list.splice(currentIndex, 1);
    return ResponseUtil.success(null, '删除成功');
  }

  public serviceYaml: any = {
    apiVersion: 'v1',
    kind: 'Service',
    metadata: {
      annotations: null,
      clusterName: null,
      creationTimestamp: '2025-09-22T17:18:10+08:00',
      deletionGracePeriodSeconds: null,
      deletionTimestamp: null,
      finalizers: null,
      generateName: null,
      generation: null,
      labels: {
        'k8s.kuboard.cn/layer': 'web',
        'k8s.kuboard.cn/name': 'nginx222',
      },
      managedFields: [
        {
          apiVersion: 'v1',
          fieldsType: 'FieldsV1',
          fieldsV1: {
            'f:metadata': {
              'f:labels': {
                '.': {},
                'f:k8s.kuboard.cn/layer': {},
                'f:k8s.kuboard.cn/name': {},
              },
            },
            'f:spec': {
              'f:externalTrafficPolicy': {},
              'f:internalTrafficPolicy': {},
              'f:ports': {
                '.': {},
                'k:{"port":80,"protocol":"TCP"}': {
                  '.': {},
                  'f:name': {},
                  'f:nodePort': {},
                  'f:port': {},
                  'f:protocol': {},
                  'f:targetPort': {},
                },
              },
              'f:selector': {},
              'f:sessionAffinity': {},
              'f:type': {},
            },
          },
          manager: 'Mozilla',
          operation: 'Update',
          subresource: null,
          time: '2025-09-22T17:18:10+08:00',
        },
      ],
      name: 'nginx222',
      namespace: 'default',
      ownerReferences: null,
      resourceVersion: '5844',
      selfLink: null,
      uid: '32c6c6ef-b160-4526-bb1c-b008aebd0a86',
    },
    spec: {
      allocateLoadBalancerNodePorts: null,
      clusterIP: '10.92.235.65',
      clusterIPs: ['10.92.235.65'],
      externalIPs: null,
      externalName: null,
      externalTrafficPolicy: 'Cluster',
      healthCheckNodePort: null,
      internalTrafficPolicy: 'Cluster',
      ipFamilies: ['IPv4'],
      ipFamilyPolicy: 'SingleStack',
      loadBalancerClass: null,
      loadBalancerIP: null,
      loadBalancerSourceRanges: null,
      ports: [
        {
          appProtocol: null,
          name: '27kz7h',
          nodePort: 32762,
          port: 80,
          protocol: 'TCP',
          targetPort: 80,
        },
      ],
      publishNotReadyAddresses: null,
      selector: {
        'k8s.kuboard.cn/layer': 'web',
        'k8s.kuboard.cn/name': 'nginx222',
      },
      sessionAffinity: 'None',
      sessionAffinityConfig: null,
      type: 'NodePort',
    },
    status: {
      conditions: null,
      loadBalancer: {
        ingress: null,
      },
    },
  };
  postGetServiceYaml(body: any) {
    return ResponseUtil.success(this.serviceYaml);
  }
}
